package com.avic.rong.adapter.product;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.product.CommentBean;
import com.avic.rong.utils.DateUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/7/15 20:22
 * E-Mail Address：16601203965@163.com
 */
public class CommentListAdapter extends RecyclerView.Adapter<CommentListAdapter.CommentViewHolder> {
    private Context context;
    private List<CommentBean.CommentsBean> commentsBeanList = new ArrayList<>();


    public CommentListAdapter(Context context, List<CommentBean.CommentsBean> commentsBeanList) {
        this.context = context;
        this.commentsBeanList = commentsBeanList;
    }

    public void setCommentsBeanList(List<CommentBean.CommentsBean> commentsBeanList) {
        this.commentsBeanList.addAll(commentsBeanList);
        notifyDataSetChanged();
    }

    public List<CommentBean.CommentsBean> getCommentsBeanList() {
        return commentsBeanList;
    }

    public void clearData(){
        this.commentsBeanList.clear();
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(this.context).inflate(R.layout.view_comment, viewGroup, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final CommentViewHolder holder, final int i) {

        CommentBean.CommentsBean bean =  commentsBeanList.get(i);
        Glide.with(this.context).load(bean.getLogo()).placeholder(R.mipmap.ic_default_head).into(holder.img_header);
        holder.tv_name.setText(bean.getUserName());
        holder.tv_content.setText(bean.getContent());
        holder.tv_time.setText(DateUtils.getFormatShort(bean.getAddTime()));

    }

    @Override
    public int getItemCount() {
        if (commentsBeanList == null){
            return 0;
        }else {
            return commentsBeanList.size();
        }
    }


    class CommentViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_header)
        ImageView img_header;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_content)
        TextView tv_content;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }



}
